#include <stdio.h>
#include "Dynamicarry.h"
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
struct Student
{
    char *name;
    int age;
};
typedef struct Student Stu;
Stu *createStu(const char *name, int age)
{
    Stu *s = (Stu *)malloc(sizeof(Stu));
    if (s == NULL)
    {
        printf("student malloc error!\n");
        return NULL;
    }
    s->age = age;
    s->name = strdup(name);
    return s;
}
void PrintStu(void *ptr)
{
    Stu *s = (Stu *)ptr;
    printf("学生姓名：%s   年龄：%d\n", s->name, s->age);
}
void FreeStu(void *ptr)
{
    Stu *s = (Stu *)ptr;
    free(s->name);
    free(s);
}
bool IsNameEqual(void *ptr1, void *ptr2)
{
    Stu *s = (Stu *)ptr1;
    char *name = (char *)ptr2;
    return strcmp(s->name, name) == 0;
}
void PrintStuArray(void*ptr)
{
    DArray*a=(DArray*)ptr;
    printf("班级===============\n");
    PrintArray(a,PrintStu);
}
typedef struct Class
{
    char *name;//班级名
    DArray* stuArray;

}Class;
Class* CreateClass(const char*name)
{
    Class*c=(Class*)malloc(sizeof(Class));
    if(c==NULL)
    {
        printf("class malloc error!\n");
    }
    c->name=strdup(name);
    c->stuArray=InitDArray(10,FreeStu);
    return c;


}
void FreeClass(void*ptr)
{
    Class*c=(Class*)ptr;
    free(c->name);
    FreeDArray(c->stuArray);

}


bool IsClassNameEql(void*ptr1,void*ptr2)
{
    Class*c=(Class*)ptr1;
    char*name=(char*)ptr2;
    return strcmp(c->name,name);
}



void InsertStu2Class(DArray*a, char *Classname,Stu*s)
{
    Class*c=(Class*)FindElement(a,Classname,IsClassNameEql);
    if(c==NULL)
    {
        printf("没有找到这个班级:%s\n",Classname);
        return;
    }
    InsertTail(c->stuArray,s);
}

void PrintClass(void*ptr)
{
Class*c=(Class*)ptr;
printf("班级名称：%s\n",c->name);
PrintArray(c->stuArray,PrintStu);


}




int main()
{

    // DArray *a = InitDArray(10, FreeStu);

    // InsertTail(a, createStu("张三", 20));
    // InsertTail(a, createStu("王五", 21));
    // InsertTail(a, createStu("赵六", 20));
    // PrintArray(a, PrintStu);
    // Stu *s = (Stu *)FindElement(a, "王五", IsNameEqual);
    // if (s == NULL)
    // {
    //     printf("没找到！\n");
    // }
    // else
    // {
    //     DeleteByElement(a, s);
    //     printf("\n");
    //     PrintArray(a, PrintStu);
    // }
    // FreeDArray(a);

    //动态二维数组
    // DArray *a=InitDArray(3,FreeDArray);
    // DArray *class1 = InitDArray(10, FreeStu);
    // InsertTail(class1,createStu("张三",20));
    // InsertTail(class1,createStu("李四",20));

    //  DArray *class2 = InitDArray(10, FreeStu);
    // InsertTail(class2,createStu("王五",20));
    // InsertTail(class2,createStu("赵六",20));
    

    // InsertTail(a,class1);
    // InsertTail(a,class2);

    // PrintArray(a,PrintStuArray);


    DArray*suqian=InitDArray(10,FreeClass);
    InsertTail(suqian,CreateClass("262班"));
    InsertTail(suqian,CreateClass("267班"));


    InsertStu2Class(suqian,"262班",createStu("张三",20));
    InsertStu2Class(suqian,"262班",createStu("李四",20));


    InsertStu2Class(suqian,"267班",createStu("王五",20));
    InsertStu2Class(suqian,"267班",createStu("赵六",20));

    PrintArray(suqian,PrintClass);
    FreeDArray(suqian);

    return 0;
}